import { compareface } from "../../utils/api";

Page({
  data: {
    left_src: "",
    right_src: "",
    left_camera: false,
    right_camera: false,
  },
  // 开始匹配
  startMatching() {
    if (this.data.left_src && this.data.right_src) {
      wx.showLoading({
        title: "匹配中",
        mask: true,
      });
      // data:image/jpeg;base64,
      let img1 =
        "data:image/jpeg;base64," +
        wx.getFileSystemManager().readFileSync(this.data.left_src, "base64");
      let img2 =
        "data:image/jpeg;base64," +
        wx.getFileSystemManager().readFileSync(this.data.right_src, "base64");

      console.log(wx.getStorageSync("token"));
      console.log("图片转化完成");
      compareface({
        data: {
          img1,
          img2,
        },
        method: "POST",
        success: (result) => {
          console.log(result);
          wx.hideLoading();
          if (result.data.score) {
            wx.navigateTo({
              url: "/pages/result/result",
              success: (res) => {
                res.eventChannel.emit("homeEvent", result.data.score);
              },
            });
          } else {
            console.log(result.data.msg);
            wx.showToast({
              title: "重新刷新页面",
              icon: "none",
              duration: 2000,
            });
          }
        },
        fail: (err) => {
          wx.hideLoading();
          wx.showToast({
            title: "匹配失败",
            duration: "none",
            duration: 2000,
          });
          console.log(err);
        },
      });
    } else {
      wx.showToast({
        title: "需要两张图片",
        icon: "none",
        duration: 2000,
      });
    }
  },
  // 点击头像
  showSheet(e) {
    let index = e.currentTarget.dataset["index"];
    wx.showActionSheet({
      itemList: ["打开相册", "打开摄像头"],
      success: (res) => {
        if (res.tapIndex == 0) {
          this.setData({
            left_camera: false,
            right_camera: false,
          });
          wx.chooseImage({
            count: 1,
            sizeType: ["original", "compressed"],
            sourceType: ["album", "camera"],
            success: (res) => {
              // tempFilePath可以作为img标签的src属性显示图片
              const tempFilePaths = res.tempFilePaths;
              if (index == 0) {
                this.setData({
                  left_src: tempFilePaths[0],
                });
              } else if (index == 1) {
                this.setData({
                  right_src: tempFilePaths[0],
                });
              }
            },
          });
        } else if (res.tapIndex == 1) {
          this.setData({
            left_camera: false,
            right_camera: false,
          });
          if (index == 0) {
            this.setData({
              left_camera: true,
            });
          } else if (index == 1) {
            this.setData({
              right_camera: true,
            });
          }
        }
      },
    });
  },
  // 拍照
  takePhoto() {
    const ctx = wx.createCameraContext();
    ctx.takePhoto({
      quality: "low",
      success: (res) => {
        if (this.data.left_camera) {
          this.setData({
            left_src: res.tempImagePath,
            left_camera: false,
          });
        } else if (this.data.right_camera) {
          this.setData({
            right_src: res.tempImagePath,
            right_camera: false,
          });
        }
      },
    });
  },
  onShareAppMessage() {
    return {
      title: "人脸相似对比",
      path: "/pages/home/home",
      imageUrl:
        "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601044157943&di=caaff53471ca72cb223c861d6a61dadd&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20171018%2Fac25c331f5f64015b69cc41c2c491393.jpeg",
    };
  },
  onShareTimeline() {
    return {
      title: "人脸相似对比",
      imageUrl:
        "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601044157943&di=caaff53471ca72cb223c861d6a61dadd&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20171018%2Fac25c331f5f64015b69cc41c2c491393.jpeg",
    };
  },
});
